<?php

namespace app\api\controller\kuyou\applet;

use think\Db;

class Cost extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['c.purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }
            if($pageStatus){
                $list = Db::name('kuyou_cost')->alias('c')
                    ->join('user a','c.preparation_id=a.id')
                    ->where($where)
                    ->page($page,$limit)
                    ->field('c.*,a.nickname as preparation')
                    ->select();
            }else{
                $list = Db::name('kuyou_cost')->alias('c')
                    ->join('user a','c.preparation_id=a.id')
                    ->where($where)
                    ->field('c.*,a.nickname as preparation')
                    ->select();
            }
            $row_count = Db::name('kuyou_cost')->alias('c')
                ->join('user a','c.preparation_id=a.id')
                ->where($where)
                ->count('c.id');

            $suppliers = Db::name('kuyou_suppliers')->column('supplyer_name','id');
            foreach ($list as $list_k=>$list_v){
                $expenditure = Db::name('kuyou_expenditure_info')->where(['cost_id'=>$list_v['id'],'business_type'=>'1'])->group('purchase_code')->field('purchase_code')->select();
                $list[$list_k]['expenditure'] = $expenditure;
                $list[$list_k]['suppliers'] = isset($suppliers[$list_v['suppliers_id']])?$suppliers[$list_v['suppliers_id']]:'';
                $list[$list_k]['suppliers_name'] = isset($suppliers[$list_v['suppliers_ids']])?$suppliers[$list_v['suppliers_ids']]:'';
                $list[$list_k]['purchase_times'] = !empty($list_v['purchase_times'])?date('Y-m-d',$list_v['purchase_times']):'';
                unset($list[$list_k]['admin_id']);
                unset($list[$list_k]['createtime']);
                unset($list[$list_k]['updatetime']);
            }

            $result = array("count" => $row_count, "list" => $list);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
